package frontend

import (
    "embed"
    "io/fs"
    "log"
    "net/http"
    "strings"
		"errors"
		"fmt"
		"os"
)

//go:generate npm i
//go:generate npm run build
//go:embed all:build
var files embed.FS

func SvelteKitHandler(path string) http.Handler {    
    fsys, err := fs.Sub(files, "build")
    if err != nil {
        log.Fatal(err)
    }
    filesystem := http.FS(fsys)
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        path := strings.TrimPrefix(r.URL.Path, path)
        // try if file exists at path, if not append .html (SvelteKit adapter-static specific)
        _, err := filesystem.Open(path)
				fmt.Println("err:", errors.Is(err, os.ErrNotExist))
				
        if errors.Is(err, os.ErrNotExist) {
            path = fmt.Sprintf("%s.html", path)
        }
				fmt.Printf("path:%+v\n", path)
        r.URL.Path = path
        http.FileServer(filesystem).ServeHTTP(w, r)
    })
}